﻿<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:designer="using:Adaptive.ReactiveTrader.Client.UI.SpotTiles.Designer"
    xmlns:i="using:Microsoft.Xaml.Interactivity" 
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:converters="using:Adaptive.ReactiveTrader.Client.Converters"
    xmlns:behaviors="using:Adaptive.ReactiveTrader.Client.Behaviors"
    xmlns:controls="using:Adaptive.ReactiveTrader.Client.Controls"
    xmlns:spotTiles="using:Adaptive.ReactiveTrader.Client.UI.SpotTiles"
    x:Class="Adaptive.ReactiveTrader.Client.UI.SpotTiles.SpotTilePricingView"
    mc:Ignorable="d"
    d:DesignHeight="150"
    d:DesignWidth="328"
    d:DataContext="{d:DesignInstance designer:DesignTimeSpotTilePricingViewModel, IsDesignTimeCreatable=True}">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Styles/Brushes.xaml" />
                <ResourceDictionary Source="/Styles/TextBox.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="PriceButtonStyle" TargetType="Button">
                <Setter Property="HorizontalAlignment" Value="Stretch" />
                <Setter Property="VerticalAlignment" Value="Stretch" />
                <Setter Property="controls:CursorHelper.Cursor" Value="Hand" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Grid Background="Transparent">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="PointerOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PriceButtonMouseOverBrush}"/>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Pressed">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PriceButtonMousePressedBrush}"/>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PipsText">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SecondaryForeground}"/>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                    <VisualStateGroup x:Name="ExecutionGroup">
                                        <VisualState x:Name="NotPriceExecuting" />
                                        <VisualState x:Name="PriceExecuting">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PriceButtonMouseOverBrush}"/>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <i:Interaction.Behaviors>
                                    <core:DataTriggerBehavior Binding="{Binding IsExecuting}" Value="True">
                                        <core:GoToStateAction StateName="PriceExecuting"/>
                                    </core:DataTriggerBehavior>
                                    <core:DataTriggerBehavior Binding="{Binding IsExecuting}" Value="False">
                                        <core:GoToStateAction StateName="NotPriceExecuting" />
                                    </core:DataTriggerBehavior>
                                </i:Interaction.Behaviors>
                                <Grid.Resources>
                                    <Style TargetType="TextBlock" BasedOn="{StaticResource ReactiveTraderBaseTextBlockStyle}" />
                                </Grid.Resources>
                                <Border x:Name="Border">
                                    <StackPanel Margin="0,5,0,0">
                                        <TextBlock Text="{Binding Direction}" HorizontalAlignment="Center" FontSize="16" Foreground="{StaticResource SecondaryForeground}" />
                                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,-8,0,0">
                                            <TextBlock Text="{Binding BigFigures}" FontSize="18" Foreground="{StaticResource SecondaryForeground}" />
                                            <TextBlock x:Name="PipsText" Text="{Binding Pips}" FontSize="48" FontWeight="Light" Foreground="{StaticResource PrimaryForeground}" Margin="5,0" />
                                            <TextBlock x:Name="TenthOfPipText" Text="{Binding TenthOfPip}" FontSize="18" Foreground="{StaticResource SecondaryForeground}" />
                                        </StackPanel>
                                    </StackPanel>
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <converters:ToStringConverter x:Key="ToStringConverter" />
        </ResourceDictionary>

    </UserControl.Resources>

    <Border x:Name="RootBorder" Padding="15,12" Background="{StaticResource TileRootBackgroundBrush}">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="DirectionGroup">
                <VisualState x:Name="Up">
                    <Storyboard>
                        <DoubleAnimation Duration="0:0:0.3" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="UpMovementIcon" d:IsOptimized="True"/>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Down">
                    <Storyboard>
                        <DoubleAnimation Duration="0:0:0.3" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="DownMovementIcon" d:IsOptimized="True"/>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="StatusGroup">
                <VisualState x:Name="Streaming"/>
                <VisualState x:Name="Subscribing">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="PriceButtonsPanel">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Collapsed</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="NotionalPanel">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Collapsed</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="SubscribingTextBlock">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Visible</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Stale">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="PriceButtonsPanel">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Collapsed</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="NotionalPanel">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Collapsed</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="StaleTextBlock">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Visible</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background)" Storyboard.TargetName="RootBorder">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ErrorBackgroundBrush}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Executing">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground)" Storyboard.TargetName="SpreadTextBlock">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SecondaryForeground}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ExecutingStatusTextBlock">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Visible</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.IsEnabled)" Storyboard.TargetName="BidButton">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <x:Boolean>False</x:Boolean>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.IsEnabled)" Storyboard.TargetName="AskButton">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <x:Boolean>False</x:Boolean>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal">
                    <Storyboard>
                        <ColorAnimation Duration="0:0:0.25" To="{StaticResource TileRootBackgroundColor}" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="RootBorder" d:IsOptimized="True"/>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="PointerOver">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="SpotDateTextBlock">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Visible</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ColorAnimation Duration="0:0:0.15" To="#FF294464" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="RootBorder" d:IsOptimized="True"/>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <i:Interaction.Behaviors>
            <behaviors:OnLoadedRefreshDataTriggersBehavior />
            <behaviors:MouseOverVisualStateBehavior />
            <core:DataTriggerBehavior Binding="{Binding IsSubscribing}" Value="True">
                <core:GoToStateAction StateName="Subscribing"/>
            </core:DataTriggerBehavior>
            <core:DataTriggerBehavior Binding="{Binding IsSubscribing}" Value="False">
                <core:GoToStateAction StateName="Streaming"/>
            </core:DataTriggerBehavior>
            <core:DataTriggerBehavior Binding="{Binding IsStale}" Value="True">
                <core:GoToStateAction StateName="Stale"/>
            </core:DataTriggerBehavior>
            <core:DataTriggerBehavior Binding="{Binding IsStale}" Value="False">
                <core:GoToStateAction StateName="Streaming"/>
            </core:DataTriggerBehavior>
            <core:DataTriggerBehavior Binding="{Binding Bid.IsExecuting}" Value="True">
                <core:GoToStateAction StateName="Executing"/>
            </core:DataTriggerBehavior>
            <core:DataTriggerBehavior Binding="{Binding Bid.IsExecuting}" Value="False">
                <core:GoToStateAction StateName="Streaming"/>
            </core:DataTriggerBehavior>
            <core:DataTriggerBehavior Binding="{Binding Ask.IsExecuting}" Value="True">
                <core:GoToStateAction StateName="Executing"/>
            </core:DataTriggerBehavior>
            <core:DataTriggerBehavior Binding="{Binding Ask.IsExecuting}" Value="False">
                <core:GoToStateAction StateName="Streaming"/>
            </core:DataTriggerBehavior>
            <core:DataTriggerBehavior Binding="{Binding Movement, Converter={StaticResource ToStringConverter}}" Value="Up">
                <core:GoToStateAction StateName="Up" />
            </core:DataTriggerBehavior>
            <core:DataTriggerBehavior Binding="{Binding Movement, Converter={StaticResource ToStringConverter}}" Value="Down">
                <core:GoToStateAction StateName="Down" />
            </core:DataTriggerBehavior>
        </i:Interaction.Behaviors>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>


            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0"
                           Text="{Binding Symbol, Mode=OneWay}" 
                           Foreground="{StaticResource PrimaryForeground}" FontSize="18" />

                <TextBlock x:Name="ExecutingStatusTextBlock" Grid.Column="2" Visibility="Collapsed"
                            Text="EXECUTING" 
                            Foreground="{StaticResource PrimaryForeground}" FontSize="18" />
            </Grid>

            <Grid x:Name="PriceButtonsPanel" Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Button x:Name="BidButton" Grid.Column="0"
					DataContext="{Binding Bid}"
					Command="{Binding ExecuteCommand}" 
					Style="{StaticResource PriceButtonStyle}" 
					Margin="0,0,5,0" />

                <StackPanel Grid.Column="1" VerticalAlignment="Center">
                    <Path x:Name="UpMovementIcon" Data="M6,0L12,12 0,12z" Fill="#FF3FCF1E" Opacity="0" Height="12" Width="12" />
                    <TextBlock x:Name="SpreadTextBlock"  Text="{Binding Spread}" FontSize="16" Foreground="{StaticResource PrimaryForeground}" Margin="3,5" />
                    <Path x:Name="DownMovementIcon" Data="M6,12L0,0 12,0z" Fill="#FFCC3350" Opacity="0" Height="12" Width="12" />
                </StackPanel>

                <Button x:Name="AskButton" Grid.Column="2" 
					DataContext="{Binding Ask}" 
					Command="{Binding ExecuteCommand}" 
					Style="{StaticResource PriceButtonStyle}" 
					Margin="5,0,0,0" />
            </Grid>

            <TextBlock x:Name="StaleTextBlock" Grid.Row="1" Visibility="Collapsed" VerticalAlignment="Top"
                            Text="Pricing currently unavailable" 
                            Foreground="{StaticResource PrimaryForeground}" FontSize="16" />

            <TextBlock x:Name="SubscribingTextBlock" Grid.Row="1" Visibility="Collapsed" VerticalAlignment="Top"
                            Text="Subscribing..." 
                            Foreground="{StaticResource SecondaryForeground}" FontSize="16" />

            <Grid x:Name="NotionalPanel" Grid.Row="2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <spotTiles:SpotTileNotionalControl Grid.Column="0" />

                <TextBlock x:Name="SpotDateTextBlock" Grid.Column="1" Text="{Binding SpotDate}" Foreground="{StaticResource SecondaryForeground}" FontSize="16" Visibility="Collapsed" />
            </Grid>
        </Grid>
    </Border>
</UserControl>
